﻿Imports System.Xml.Serialization
Imports System.IO

Public Class DailyReportCommon
    Public Enum UserStatus As Integer
        正常 = 1
        冻结 = 2
        离职 = 3
    End Enum

    Public Enum PermissionList As Integer
        一审 = 1      'CheckLevel1
        二审 = 2      'CheckLevel2
        三审 = 4      'CheckLevel3
        审核会签 = 8    'CheckLevel4
        地价会签 = 16   'CheckLevel5
        审核报表 = 32   'CheckReport
        评估工作报告 = 64     'AssessInput
        评估报表 = 128          'AssessReport
        总收发工作报告 = 256   'TransceiverInput
        总收发报表 = 512     'TranscsiverReport
        系统设置 = 1024     'System
    End Enum

    Shared Function GetMaskList() As ParmissionsModel
        Dim pm As New ParmissionsModel
        Try
            If (IsNothing(HttpContext.Current.Cache("DR_MaskList"))) Then
                Using fs As New StreamReader(HttpContext.Current.Server.MapPath("~\Setting\Permissions.xml"))
                    Dim xs As New XmlSerializer(GetType(ParmissionsModel))
                    pm = xs.Deserialize(fs)
                End Using
                HttpContext.Current.Cache("DR_MaskList") = pm
            Else
                pm = HttpContext.Current.Cache("DR_MaskList")
            End If
        Catch ex As Exception

        End Try
        Return pm
    End Function

    Shared Function MatchingPermission(ByVal permissions As Integer, ByVal mask As Integer) As Boolean
        Try
            If (permissions And mask) = mask Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function

    Shared Function GetPermissionList(ByVal permissions As Integer) As List(Of Integer)
        Dim pl As New List(Of Integer)
        Try
            For Each p In GetMaskList.PermissionList
                If ((permissions And p.Mask) = p.Mask) Then
                    pl.Add(p.Mask)
                End If
            Next
            Return pl
        Catch ex As Exception
            Return pl
        End Try
    End Function

    Shared Function FormatChineseDate(ByVal indate As Date) As String
        Try
            Dim sy As String = ""
            Dim sm As String = ""

            For Each s In indate.Year.ToString().ToCharArray
                sy += FormatNum(s)
            Next

            sm = FormatMonth(indate.Month)

            Return String.Concat(sy, "年", sm, "月")
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Shared Function FormatNum(ByVal innum As String) As String
        Try
            Dim result As String = ""
            Select Case innum
                Case "0"
                    result = "〇"
                Case "1"
                    result = "一"
                Case "2"
                    result = "二"
                Case "3"
                    result = "三"
                Case "4"
                    result = "四"
                Case "5"
                    result = "五"
                Case "6"
                    result = "六"
                Case "7"
                    result = "七"
                Case "8"
                    result = "八"
                Case "9"
                    result = "九"
            End Select
            Return result
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Shared Function FormatMonth(ByVal innum As String) As String
        Try
            Dim result As String = ""
            Select Case innum
                Case 1
                    result = "一"
                Case 2
                    result = "二"
                Case 3
                    result = "三"
                Case 4
                    result = "四"
                Case 5
                    result = "五"
                Case 6
                    result = "六"
                Case 7
                    result = "七"
                Case 8
                    result = "八"
                Case 9
                    result = "九"
                Case 10
                    result = "十"
                Case 11
                    result = "十一"
                Case 12
                    result = "十二"
            End Select
            Return result
        Catch ex As Exception
            Return ""
        End Try
    End Function

#Region "业务相关"
    Shared Function GetBusinessList() As Businesses
        Dim bm As New Businesses
        Try
            If (IsNothing(HttpContext.Current.Cache("DR_BusinessList"))) Then
                Using fs As New StreamReader(HttpContext.Current.Server.MapPath("~\Setting\Business.xml"))
                    Dim xs As New XmlSerializer(GetType(Businesses))
                    bm = xs.Deserialize(fs)
                End Using
                HttpContext.Current.Cache("DR_BusinessList") = bm
            Else
                bm = HttpContext.Current.Cache("DR_BusinessList")
            End If
        Catch ex As Exception

        End Try
        Return bm
    End Function
#End Region

#Region "從Cacht裡面Get翻當前用戶信息"
    Shared ReadOnly Property CurrentUserID As String
        Get
            Try
                Dim ina = HttpContext.Current.User.Identity.Name.Split("|")
                Return ina(0)
            Catch ex As Exception
                Return ""
            End Try
        End Get
    End Property
    Shared ReadOnly Property CurrentUserName As String
        Get
            Try
                Dim ina = HttpContext.Current.User.Identity.Name.Split("|")
                Return ina(1)
            Catch ex As Exception
                Return ""
            End Try
        End Get
    End Property
    Shared ReadOnly Property CurrentUserNum As String
        Get
            Try
                Dim ina = HttpContext.Current.User.Identity.Name.Split("|")
                Return ina(2)
            Catch ex As Exception
                Return ""
            End Try
        End Get
    End Property
    Shared ReadOnly Property CurrentDepartmentID As String
        Get
            Try
                Dim ina = HttpContext.Current.User.Identity.Name.Split("|")
                Return ina(3)
            Catch ex As Exception
                Return ""
            End Try
        End Get
    End Property
    Shared ReadOnly Property CurrentUserPermissions As Integer
        Get
            Try
                Dim p As Integer = 0
                Dim ina As String = HttpContext.Current.User.Identity.Name.Split("|")(4).ToString
                Integer.TryParse(ina, p)

                Return p
            Catch ex As Exception
                Return 0
            End Try
        End Get
    End Property
#End Region

#Region "枚举"
    Public Enum ReportGrain As Integer
        Day = 1
        Month = 2
    End Enum
#End Region
End Class
